package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsImpl implements NewsDao {
    @Override
    public List<NewsModel> getNewsModelList(PageModel pageModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.getNewsModelList", pageModel);
        GetSqlSession.commit();
        return list;
    }

    @Override
    public List<NewsModel> getNewsModelForHome() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.getNewsModelForHome");
        GetSqlSession.commit();
        return list;
    }

    @Override
    public Integer getNewsCount() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        int count = sqlSession.selectOne("com.cn.wanxi.dao.NewsDao.getNewsCount");
        GetSqlSession.commit();
        return count;
    }

    /**
     * 新增新闻
     * @param newsModel 新闻详细
     * @return 1成功  0失败
     */
    @Override
    public Integer addNews(NewsModel newsModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        int result = sqlSession.insert("com.cn.wanxi.dao.NewsDao.addNews", newsModel);
        GetSqlSession.commit();
        return result;
    }

    @Override
    public NewsModel findById(NewsModel newsModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        NewsModel model = sqlSession.selectOne("com.cn.wanxi.dao.NewsDao.findById", newsModel);
        GetSqlSession.commit();
        return model;
    }

    @Override
    public List<NewsModel> findNewsBySome(NewsModel newsModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.findNewsBySome", newsModel);
        GetSqlSession.commit();
        return list;
    }
}
